/* 
 * GrayRabbit Cms基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department：运营部
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.dao.system.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.grayrabbit.cms.dao.system.RoleDao;
import org.grayrabbit.cms.entity.Role;
import org.grayrabbit.cms.vo.system.RoleBean;
import org.springframework.stereotype.Repository;

import org.grayrabbit.dao.impl.GenericHibernateDao;
import org.grayrabbit.dao.util.Page;
import org.grayrabbit.dao.util.QueryCondition;

/**
 * <b>Role</b> dao implementation 





 * @version 1.0，创建时间：2011-05-16 15:55:03 
 */
@Repository
public class RoleDaoImpl extends GenericHibernateDao<Role, String>
		implements RoleDao {

	/**
	 * HQL主体语句
	 */
	public static final String MAIN_HQL = "From Role as obj";
	/**
	 * HQL中count统计hql片段
	 */
	public static final String COUNT_HQL = "Select Count(obj) ";

	/**
	 * 根据bean组装查询条件
	 * @see RoleDao#assembly(RoleBean)
	 */
	public QueryCondition assembly(RoleBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件
	 * @see RoleDao#assemblyCount(RoleBean)
	 */
	public QueryCondition assemblyCount(RoleBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * @see RoleDao#count(RoleBean)
	 */
	public int count(RoleBean bean) {
		return ((Long)iterator(assemblyCount(bean)).next()).intValue();
	}

	/**
	 * 根据bean组装查询条件(名称参数)
	 * @see RoleDao#assemblyNameQuery(RoleBean)
	 */
	public QueryCondition assermblyNameQuery(RoleBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean, qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件(名称参数)
	 * @see RoleDao#assemblyNameQueryCount(RoleBean)
	 */
	public QueryCondition assemblyNameQueryCount(RoleBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 填充分页组件
	 * @see RoleDao#findByPage(Page, RoleBean)
	 */
	public List<Role> findByPage(Page<Role> page,
			RoleBean bean) {
		QueryCondition qc = assembly(bean);
		qc.setMaxResults(page.getPageSize());
		qc.setOffset(page.getOffset());
		return findByQc(qc);
	}
	// 业务查询主要逻辑代码
	private void transNameQuery(RoleBean bean,QueryCondition qc){
		if(bean!=null){
			if(StringUtils.isNotBlank(bean.getKeyword())){
				qc.append(" And ( 1!=1 ");
				String[] keyword1=bean.getKeyword().split(" ");
				for(String str:keyword1){
					if(StringUtils.isNotBlank(str)){
						qc.add(" or obj.roleName like ?", "%"+str.trim()+"%");
						qc.add(" or obj.memo like ?", "%"+str.trim()+"%");
					}
				}
				qc.append(" )");
			}
			if(bean.getActiveStatus()!=null){
				qc.add(" And obj.activeStatus =?", bean.getActiveStatus());
			}
			if(bean.getCreateTimeStart()!=null){
				qc.add(" And obj.createTime >=? ", bean.getCreateTimeStart());
			}
			if(bean.getCreateTimeEnd()!=null){
				qc.add(" And obj.createTime <=?",bean.getCreateTimeEnd());
			}
			if(StringUtils.isNotBlank(bean.getRoleName())){
				qc.add("And obj.roleName = ? ", "%"+bean.getRoleName()+"%");
			}
            if(StringUtils.isNotBlank(bean.getMemo())){
				qc.add("And obj.memo like ? ","%"+ bean.getMemo()+"%");
			}
            if(bean.getCreateTime() != null){
				qc.add("And obj.createTime = ? ", bean.getCreateTime());
			}
            if(StringUtils.isNotBlank(bean.getCreateLoginName())){
				qc.add("And obj.createLoginName like ? ", bean.getCreateLoginName());
			}
            if(StringUtils.isNotBlank(bean.getCreateRealName())){
				qc.add("And obj.createRealName like ? ", "%"+bean.getCreateRealName()+"%");
			}
            if(StringUtils.isNotBlank(bean.getUpdateLoginName())){
				qc.add("And obj.updateLoginName = ? ", bean.getUpdateLoginName());
			}
            if(StringUtils.isNotBlank(bean.getUpdateRealName())){
				qc.add("And obj.updateRealName like ? ", "%"+bean.getUpdateRealName()+"%");
			}
            if(bean.getUpdateTime() != null){
				qc.add("And obj.updateTime = ? ", bean.getUpdateTime());
			}
            qc.appenOrderBy(" nlssort(obj.roleName,'NLS_SORT=SCHINESE_PINYIN_M')");
		}
	}

	/**
	 * 检查角色名称唯一性
	 */
	public boolean checkNameUnique(String  roleName,String roleId){
		boolean unique=false;
		List<Role> roleList=null;
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		if(StringUtils.isNotBlank(roleName)){
			qc.add(" And obj.roleName =?", roleName);
			roleList=findByQc(qc);
		}
		if(StringUtils.isNotBlank(roleId)){
			if(roleList!=null&&(!roleList.isEmpty())){
			if(!roleList.get(0).getRoleId().equals(roleId)){
				unique=true;
				}
			}
		}else{
			if((roleList!=null)&&(!roleList.isEmpty())){
				unique=true;
			}
		}
		return unique;
	}

}